<template>
	<el-switch
			v-if="show"
			v-model="modelValue_"
			:active-text="props.showText ? activeBusinessCodeItem.label : undefined"
			:active-value="activeBusinessCodeItem.value"
			:before-change="props.beforeChange"
			:disabled="props.disabled"
			:inactive-text="props.showText ? inActiveBusinessCodeItem.label : undefined"
			:inactive-value="inActiveBusinessCodeItem.value"
			:loading="props.loading"
			v-bind="$attrs"
	></el-switch>
</template>

<script lang="ts" setup>

import {computed} from 'vue'
import {getBusinessCodeData} from '@/utils/commonUtil'

interface Props {
	codeType: string
	modelValue: BusinessCodeSwitchValue
	activeValue?: BusinessCodeSwitchValue
	inactiveValue?: BusinessCodeSwitchValue
	hideValue?: boolean
	loading?: boolean
	disabled?: boolean
	showText?: boolean
	forceStringValue?: boolean
	beforeChange?: () => boolean | Promise<boolean>
}

interface BusinessCodeItemInner {
	value: BusinessCodeSwitchValue
	label: string
}

const props = withDefaults(defineProps<Props>(), {
	activeValue: true,
	inactiveValue: false,
	hideValue: true,
	loading: false,
	disabled: false,
	showText: true,
	forceStringValue: false
})

const emits = defineEmits<{
	(e: 'update:modelValue', value?: BusinessCodeSwitchValue): void
	(e: 'change', value?: BusinessCodeSwitchValue): void
}>()

const modelValue_ = computed({
	get: () => props.modelValue,
	set: (value: string) => {
		emits('update:modelValue', value)
		emits('change', value)
	}
})

const show = computed(() => {
	if (businessCodeData.value) {
		if (businessCodeData.value.length != 2) {
			console.error(`选项数量不为2，无法使用business-code-switch组件：业务编码【${props.codeType}】的选项数量为${businessCodeData.value.length}`)
			return false
		} else {
			return true
		}
	} else {
		console.error(`无法获取业务编码【${props.codeType}】的数据`)
		return false
	}
})

const businessCodeData = computed(() => getBusinessCodeData(props.codeType))
const activeBusinessCodeItem = computed<BusinessCodeItemInner>(() => {
	const find = businessCodeData.value.find(item => handleValue(item.value) === props.activeValue)
	if (find) {
		return {value: handleValue(find.value), label: props.hideValue ? find.label : `${find.value} : ${find.label}`}
	} else {
		return {value: handleValue('1'), label: ''}
	}
})

const inActiveBusinessCodeItem = computed<BusinessCodeItemInner>(() => {
	const find = businessCodeData.value.find(item => handleValue(item.value) === props.inactiveValue)
	if (find) {
		return {value: handleValue(find.value), label: props.hideValue ? find.label : `${find.value} : ${find.label}`}
	} else {
		return {value: handleValue('0'), label: ''}
	}
})

const handleValue = (value: string): BusinessCodeSwitchValue => {
	if (props.forceStringValue) {
		return value
	}
	// 尝试转换为boolean
	if (value === 'true' || value === 'false') {
		return JSON.parse(value)
	}
	// 尝试转换为number
	const numberValue = Number(value)
	if (Number.isSafeInteger(numberValue)) {
		return numberValue
	}
	// 其他情况使用string
	else {
		return value
	}
}

</script>

<style lang="scss" scoped>
</style>
